﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using PeaceBlogData;
using PeaceBlogData.Tables;

namespace PeaceBlog.Controllers.API
{
    public class PagesController : ApiController
    {
        private PeaceBlogEntity db = new PeaceBlogEntity();

        // GET: api/Pages
        [HttpGet]
        public async Task<List<Pages>> GetPages()
        {
            return await db.GetAllPages();
        }

        // GET: api/Pages/5
        [HttpGet]
        public async Task<Pages> GetPages(Guid id)
        {
            var pages = await db.GetPage(id);
            if (pages == null)
            {
                return null;
            }

            return pages;
        }

        // PUT: api/Pages/5
        [HttpPut]
        public async Task<IHttpActionResult> UpdatePages(Pages pages)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                await db.UpdatePage(pages);
            }
            catch (DbUpdateConcurrencyException)
            {
                return NotFound();
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Pages
        [HttpPost]
        public async Task<IHttpActionResult> AddPages(Pages pages)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            try
            {
                await db.AddPage(pages);
            }
            catch
            {
                return NotFound();
            }

            return Ok();
        }

        // DELETE: api/Pages/5
        [HttpDelete]
        public async Task<IHttpActionResult> DeletePages(Guid id)
        {
            try
            {
                await db.DeletePage(id);
            }
            catch
            {
                return NotFound();
            }
            return Ok();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}